package com.greenDao.activity.check;

import java.util.List;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.greenDao.activity.R;
import com.greenDao.activity.tool.DatabaseUtils;
import com.greenDao.activity.tool.GuiUtils;
import com.greenDao.activity.tool.LogUtils;
import com.greenDao.activity.tool.Marker;
import com.greenDao.activity.tool.Utils;
import com.greenDao.activity.tool.button.BackButton;
import com.greenDao.activity.tool.button.movie.ModifyMovieButton;
import com.greenDao.activity.tool.button.movie.RemoveMovieButton;
import com.greenDao.pojo.Movie;

public class CheckMovieActivity extends AbstractCheckActivity {

	private static SparseIntArray positionToId = new SparseIntArray();
	private static Marker marker = new Marker();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check_movie);
		setUpActionBar("Movies:	");

		GuiUtils.hookButton(this, R.id.fragmBack, new BackButton(this));
	}

	void updateActionBar(ActionBar actionBar) {
		List<Movie> movies = DatabaseUtils.getAllMovies();
		String[] titles = Utils.extractDataFromMovies(true, movies, positionToId);
		marker.updateLimit(titles.length);
		actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
				android.R.layout.simple_list_item_1, android.R.id.text1, titles), this);
		actionBar.setSelectedNavigationItem(marker.getMarker());
	}

	@Override
	public boolean onNavigationItemSelected(int position, long id) {

		Movie movie = DatabaseUtils.findMovieById(Long.valueOf(positionToId.get(position)));
		MovieSectionFragment fragment = new MovieSectionFragment();
		fragment.setMovie(movie);
		fragment.setCheckMovieActivity(this);
		marker.setMarker(position);
		getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();

		return true;
	}

	public static class MovieSectionFragment extends Fragment {

		private Movie movie;
		private CheckMovieActivity checkMovieActivity;

		public MovieSectionFragment() {
		}

		public void setMovie(Movie movie) {
			this.movie = movie;
		}

		public void setCheckMovieActivity(CheckMovieActivity checkMovieActivity) {
			this.checkMovieActivity = checkMovieActivity;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

			View rootView = inflater.inflate(R.layout.fragment_check_movie, container, false);

			GuiUtils.setTextView(rootView, R.id.fragmTitle, movie.getTitle());
			GuiUtils.setTextView(rootView, R.id.fragmDir, movie.getDirector());
			GuiUtils.setTextView(rootView, R.id.fragmYear, movie.getYear());
			GuiUtils.setTextView(rootView, R.id.fragmRate, parseRate());

			LogUtils.logMovieEntity(movie);
			
			GuiUtils.hookButton(rootView, R.id.fragmModify, new ModifyMovieButton(this, movie));
			GuiUtils.hookButton(rootView, R.id.fragmRemove, new RemoveMovieButton(checkMovieActivity, movie));

			return rootView;
		}

		private String parseRate() {
			return (movie.getRate() != null) ? Double.toString(movie.getRate()) : "not rated yet";
		}
	}
}
